require "00_common"

INPUT_DIR = "./facebook_mobile_payload/"

REG_EXPRESS = /(\/)([a-zA-Z0-9_-]+)\.php/



module GetMFacebookURL
  def self.get_all_files
    Common.get_all_files(INPUT_DIR)
  end

  def self.handle_all_files(all_files)
    facebook_url_mapping = {}

    all_files.each do |file_name|
      # return_array =
      mapping = handle_each_file file_name

      # facebook_id_array = facebook_id_array + return_array.uniq
      mapping.each do |key, value|
        if facebook_url_mapping.has_key? key
          facebook_url_mapping[key] = facebook_url_mapping[key] + value
        else
          facebook_url_mapping[key] = value
        end
      end
    end

    # facebook_id_array.uniq
    facebook_url_mapping
  end

  def self.handle_each_file(file_name)
    puts "now handling : " + file_name
    file = File.open INPUT_DIR + file_name, "r"

    facebook_url_mapping = {}

    begin
      while line = file.readline
        fields = line.split("|")

        local_time_stamp    = Common.check_string fields[0]
        session_id          = Common.check_string fields[1]
        session_start_time  = Common.check_string fields[2]
        server_ip           = Common.check_string fields[3]
        server_port         = Common.check_string fields[4]
        client_ip           = Common.check_string fields[5]
        client_port         = Common.check_string fields[6]
        referer             = Common.check_string fields[7]
        host                = Common.check_string fields[8]
        content_type        = Common.check_string fields[9]
        content_length      = Common.check_string fields[10]
        user_agent          = Common.check_string fields[11]
        connection          = Common.check_string fields[12]
        c_request           = Common.check_string fields[13]
        s_response          = Common.check_string fields[14]
        cookie              = Common.check_string fields[15]

        if line.include? "touch.facebook.com"||"m.facebook.com"
           if REG_EXPRESS.match c_request
              url_name = $2
            if facebook_url_mapping.has_key? url_name
              facebook_url_mapping[url_name] = facebook_url_mapping[url_name] + 1
             else
              facebook_url_mapping[url_name] = 1
            end
          end
        end
      end


    rescue Exception => e
      puts e.to_s
    end

    facebook_url_mapping
  end

  def self.run
    puts ">> start to get facebook urls ..."

    begin
      all_files = GetMFacebookURL.get_all_files
      all_mapping = GetMFacebookURL.handle_all_files all_files

      output = File.open "facebook_mobile_urls.csv", "w"
      all_mapping.each { |key, value| output.puts "#{key.to_s},#{value}" }
      output.close

      puts ">> end to get facebook IDs"
    rescue Exception => e
      puts ">> EEROR:"
      puts e.to_s
    end
  end
end